use crate::error::Result;
use crate::logic::common::redis::RedisService;

use crate::config::config::ApplicationConfig;
use futures::future::BoxFuture;
use std::time::Duration;

pub trait ICacheService: Sync + Send {
    fn set_str(&self, key: &str, value: &str) -> BoxFuture<Result<String>>;
    fn get_str(&self, key: &str) -> BoxFuture<Result<String>>;
    fn set_str_exp(
        &self,
        key: &str,
        value: &str,
        exp: Option<Duration>,
    ) -> BoxFuture<Result<String>>;
    fn del_str(&self, key: &str) -> BoxFuture<Result<u8>>;
    fn ttl(&self, key: &str) -> BoxFuture<Result<i64>>;
}

pub struct CacheService {
    pub inner: Box<dyn ICacheService>,
}

impl CacheService {
    pub fn new(cfg: &ApplicationConfig) -> Result<Self> {
        let inner = Box::new(RedisService::new(&cfg.redis_url));
        Ok(Self { inner })
    }
}
